package com.xiaotu.spider.project.pageprocessor;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.xiaotu.common.util.FileUtil;
import com.xiaotu.common.util.GsonUtils;
import com.xiaotu.common.util.HttpUtils;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.common.util.SepratorUtil;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

/**
 * @类名 IQYNewDocPageProcessor
 * @日期 2017年4月5日
 * @作者 高海军
 * @功能 爱奇艺纪录片爬取（更新中的纪录片）
 */
@Component("IQYNewDocPageProcessor")
public class IQYNewDocPageProcessor extends IQYDocPageProcessor
{
	@Override
	protected void detailProcess(Page page)
	{
		String[] baseInfo = this.getBaseInfo(page);
		if (baseInfo == null)
			return;
		List<Map<String, Object>> videoList = getVideoList(page);
		if (videoList == null)
			return;
		videoList.parallelStream().forEach(map ->
		{
			String[] arr = (String[]) ArrayUtils.addAll(
					new String[] { baseInfo[0], baseInfo[1], baseInfo[2],
							baseInfo[3], baseInfo[4], baseInfo[5],
							baseInfo[7] },
					new String[] { this.getVideoName(map),
							map.get("play").toString(),
							baseInfo[baseInfo.length - 1] });
			String line = StringUtils.join(arr, CSV_SPLITOR);
			System.out.println(line);
			FileUtil.writeFile("D://iqy_探索2.csv",
					line + SepratorUtil.LINE_SEPRATOR);
		});
	}
	
	private String getVideoName(Map<String, Object> map)
	{
		return StringUtils.isEmpty(map.get("tvSbtitle").toString())
				? (StringUtils.isEmpty(map.get("videoName").toString())
						? (StringUtils.isEmpty(map.get("shortTitle").toString())
								? StringUtils.EMPTY
								: map.get("shortTitle").toString())
						: map.get("videoName").toString())
				: map.get("tvSbtitle").toString();
	}
	
	@SuppressWarnings("unchecked")
	private List<Map<String, Object>> getVideoList(Page page)
	{
		Html html = page.getHtml();
		String dataId = this.getDataId(html);
		String data = HttpUtils
				.httpGetJSONP("http://cache.video.iqiyi.com/jp/sdvlst/3/"
						+ dataId + "/?categoryId=3&sourceId=" + dataId
						+ "&callback=window.Q.__callbacks__.cbzdoz7e");
		if (StringUtils.isEmpty(data))
			return null;
		data = data.substring(data.indexOf("(") + "(".length(),
				data.indexOf(");"));
		if (StringUtils.isEmpty(data))
			return null;
		Map<String, Object> map = GsonUtils.fromJson(data, Map.class);
		List<Map<String, Object>> dataList = (List<Map<String, Object>>) map
				.get("data");
		if (dataList == null)
			return null;
		StringBuffer buffer = new StringBuffer();
		List<Map<String, Object>> list = dataList.parallelStream()
				.filter(dataMap ->
				{
					boolean flag = dataMap.get("tvYear").toString()
							.startsWith("2017");
					if (flag)
					{
						long val = new BigDecimal(
								dataMap.get("tvId").toString()).longValue();
						buffer.append(SepratorUtil.SEP_COMMA_EN + val);
					}
					return flag;
				}).collect(Collectors.toList());
		if (buffer.length() < 1)
			return list;
		data = HttpUtils.httpGetJSONP("http://cache.video.iqiyi.com/jp/pc/"
				+ buffer.substring(SepratorUtil.SEP_COMMA_EN.length())
				+ "/?src=760859ef3a0046e0932d0381e641cbb6&callback=window.Q.__callbacks__.cb32z5f9");
		if (StringUtils.isEmpty(data))
			return list;
		data = data.substring(data.indexOf("(") + "(".length(),
				data.indexOf(");"));
		if (StringUtils.isEmpty(data))
			return null;
		List<Map<String, Double>> scoreList = GsonUtils.fromJson(data,
				List.class);
		if (scoreList.isEmpty())
			return list;
		Map<String, Double> scoreMap = scoreList.parallelStream()
				.collect(Collectors.toMap(
						scMap -> scMap.keySet().iterator().next(),
						scMap -> scMap.values().iterator().next()));
		list.parallelStream()
				.forEach(tvMap -> tvMap.put("play", scoreMap.get(
						new BigDecimal(tvMap.get("tvId").toString()).longValue()
								+ StringUtils.EMPTY)));
		return list;
	}
	
	@Override
	protected String getDataId(Html html)
	{
		return html.xpath("//div[@id='upDownWrap']/@data-qpaid").get();
	}
	
	@Override
	protected boolean docFilter(String divStr)
	{
		String label = new Html(divStr)
				.xpath("//span[@class='icon-vInfo']/text()").get().trim();
		return RegexUtils.regexMatch("更新至([0-9]{4}-[0-9]{2}-[0-9]{2})期", label);
	}
	
	@Override
	protected String getIntVal(String val, Page page)
	{
		return val;
	}
	
	@Override
	protected String[] getFieldKey()
	{
		return new String[] { "地区", "类型", "更新至" };
	}
	
}
